﻿using System.Collections.Generic;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Items.Placeable.FishingCrate
{
    public class SmugglingCrate : ModItem
    {
        private int nameChangeTimer = 0;
        private const int NameChangeInterval = 90;
        public override void SetStaticDefaults()
        {
            ItemID.Sets.IsFishingCrate[Type] = true;
            ItemID.Sets.OpenableBag[Type] = true;
            Item.ResearchUnlockCount = 10;
        }
        public override void SetDefaults()
        {
            Item.width = 12;
            Item.height = 12;
            Item.rare = ItemRarityID.Blue;
            Item.maxStack = Item.CommonMaxStack;
            Item.value = Item.buyPrice(0, 5, 10);
            Item.createTile = ModContent.TileType<Tiles.FishingCrate.SmugglingCrate>();
            Item.placeStyle = 0;
            Item.useAnimation = 15;
            Item.useTime = 15;
            Item.autoReuse = true;
            Item.useStyle = ItemUseStyleID.Swing;
            Item.consumable = true;
        }
        public override void UpdateInventory(Player player)
        {
            UpdateNameEffect();
        }

        public override void Update(ref float gravity, ref float maxFallSpeed)
        {
            UpdateNameEffect();
        }

        private void UpdateNameEffect()
        {
            nameChangeTimer++;
            if (nameChangeTimer >= NameChangeInterval)
            {
                nameChangeTimer = 0;               
            }
        }
        public override void ModifyTooltips(List<TooltipLine> tooltips)
        {
            string originalName = Lang.GetItemNameValue(Type);
            foreach (TooltipLine line in tooltips)
            {
                if (line.Mod == "Terraria" && line.Name == "ItemName")
                {
                    if (Main.rand.NextFloat() < 0.3f)
                    {
                        line.Text = ScrambleText(originalName);                   
                    }
                    else
                    {
                        line.Text = originalName;
                    }
                }
            }
        }
        private static string ScrambleText(string text)
        {
            char[] chars = text.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                if (Main.rand.NextBool(3)) 
                {
                    chars[i] = (char)Main.rand.Next(0x2580, 0x259F); 
                }
            }
            return new string(chars);
        }

        public override void RightClick(Player player)
        {
            int fishTypesCount = Main.rand.Next(1, 4);
            for (int i = 0; i < fishTypesCount; i++)
            {
                int fishID = ValidItemIDs[Main.rand.Next(ValidItemIDs.Length)];
                int stackSize = Main.rand.Next(1, 5);
                player.QuickSpawnItem(player.GetSource_OpenItem(Type), fishID, stackSize);
            }
        }
        private static readonly int[] ValidItemIDs =
        [
            ItemID.ArmoredCavefish,
            ItemID.AtlanticCod,
            ItemID.Bass,
            ItemID.ChaosFish,
            ItemID.CrimsonTigerfish,
            ItemID.Damselfish,
            ItemID.DoubleCod,
            ItemID.Ebonkoi,
            ItemID.FlarefinKoi,
            ItemID.Flounder,
            ItemID.FrostMinnow,
            ItemID.GoldenCarp,
            ItemID.Hemopiranha,
            ItemID.NeonTetra,
            ItemID.Obsidifish,
            ItemID.PrincessFish,
            ItemID.Prismite,
            ItemID.RedSnapper,
            ItemID.Salmon,
            ItemID.SpecularFish,
            ItemID.Stinkfish,
            ItemID.Trout,
            ItemID.Tuna,
            ItemID.VariegatedLardfish
        ];
    }
}
